import request from '@/utils/request'
import axios from "axios";
// 查询评论列表
export function getCommentByVid(vid) {
    return request({
        url: '/api/comments/movie/' + vid,
        method: 'get',
    })
}
// 新增评论
export function addComment() {
    return request({
        url: '/api/comments',
        method: 'post',
    })
}


// 删除评论
export function delComment(cid) {
    return request({
        url: '/api/comments' + cid,
        method: 'delete'
    })
}
export function getCommentList() {
    return request({
        url: '/api/comments',
        method: 'get',
    })
}
const BASE_URL = 'http://localhost:8080/api/comments';

const handleError = (error) => {
    console.error(error);
    alert('发生错误，请稍后重试！');
};

export default {
    async submitComment(vid, content) {
        if (!content) {
            alert('评论内容不能为空！');
            return;
        }

        const commentData = {
            movie: { vid },
            content
        };

        try {
            const response = await axios.post(BASE_URL, commentData);
            return response.data;
        } catch (err) {
            handleError(err);
        }
    },

    async fetchComments(id) {
        try {
            const response = await axios.get(`${BASE_URL}/movie/${id}`);
            return response.data;
        } catch (err) {
            handleError(err);
        }
    },

    async fetchAllComments() {
        try {
            const response = await axios.get(`${BASE_URL}/all`);
            return response.data;
        } catch (err) {
            handleError(err);
        }
    },

    async deleteComment(cid) {
        try {
            await axios.delete(`${BASE_URL}/${cid}`);
        } catch (err) {
            handleError(err);
        }
    }
};
